package com.example.config;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.example.pojo.Course;
import com.example.pojo.CourseReservation;
import com.example.pojo.School;
import com.example.pojo.dto.CourseDto;
import com.example.service.CourseReservationService;
import com.example.service.CourseService;
import com.example.service.SchoolService;
import lombok.AllArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@AllArgsConstructor
public class CourseTools {
    private final CourseService courseService;
    private final SchoolService schoolService;
    private final CourseReservationService courseReservationService;
    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(required = false,description = "课程查询条件") CourseDto courseDto) {
        QueryChainWrapper<Course> wrapper = courseService.query();
        wrapper.eq(courseDto.getType() != null, "type", courseDto.getType());
        wrapper.eq(courseDto.getEdu() != null, "edu", courseDto.getEdu());
        if (courseDto.getSorts() != null){
            for (CourseDto.Sort sort : courseDto.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        return wrapper.list();
    }
    @Tool(description = "查询所有校区")
    public List<School> queryAllSchools(){
        return schoolService.list();
    }
    @Tool(description = "生成课程预约单，并返回生成的预约单号")
    public String generateCourseReservation(
            String courseName,
            String contactInfo,
            String school,
            String remark,
            String studentName
    ){
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(courseName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setSchool(school);
        courseReservation.setStudentName(studentName);
        courseReservation.setRemark(remark);
        courseReservationService.save(courseReservation);
        return String.valueOf(courseReservation.getId());
    }
}
